import api from '@/api/index.js'
import {showToast} from 'vant'
// const payURLwx = import.meta.env. VITE_test_PAY_URL_WX
// const payURL = import.meta.env. VITE_test_PAY_URL

export const goPayUrl = (data) => {
    const payId = data?.payId
    // http://pay.028wlkj.com/pay/index
    // http://pay.028wlkj.com/pay/index-wx
    // 使用模板字符串来构建包含动态 payId 的 URL
    const isWeChat = /MicroMessenger/i.test(window.navigator.userAgent)
    const path = window.location.origin
    const locationgo = ` ${path}${'/order'}`
    if (isWeChat) {
        let url = `http://pay.028wlkj.com/pay/index-wx?payId=${payId}&finishUrl=${locationgo}`
        window.location.href = url
    } else {
        let url = `http://pay.028wlkj.com/pay/index?payId=${payId}&finishUrl=${locationgo}`
        // 如果不是微信浏览器
        window.location.href = url
    }
}
export default {
    getPayld(data) {


        console.log('data', data)
        var {orderNumber, type, finishUrl, backUrl} = data
        backUrl = backUrl || location.href
        let getPayId = api.getPayld
        if (type === 'hotel') {
            getPayId = api.getHotelPayId
        }
        getPayId({
            orderNumber, finishUrl, backUrl

        }).then((res) => {
            console.log("payId", res.data.payNumber)
            // 调用聚合支付
            if (res.code == 200 && res.data.payNumber) {
                goPayUrl({
                    payId: res.data.payNumber
                })
            } else {

                showToast('支付失败')


            }


        })
            .catch((error) => {
                // 处理错误
                console.error(error)
                showToast('支付失败了')

            })
    }

}


